package com.controller;

import com.entity.CourseType;
import com.github.pagehelper.PageInfo;
import com.service.CourseTypeService;
import com.util.AjaxResult;
import com.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ct")
public class CourseTypeCtl {
    @Autowired
    CourseTypeService courseTypeService;

    //查询所有课程类型
    @RequestMapping("/findCourseType.do")
    @ResponseBody
    public Map findCourseType() {
        return courseTypeService.selectAllCoursesType();
    }

    @ResponseBody
    @RequestMapping("/findByParentId.do")
    public List<List<CourseType>> findByParentId(@RequestParam("parent_id") Integer parentID) {
        return courseTypeService.selectTypeByParentId(parentID);
    }


    //    后台显示课程分类名字 pj
    @RequestMapping("showBackName.do")
    @ResponseBody
    public AjaxResult showBackName(String id) {
        System.out.println("qaq------------------------");
        return courseTypeService.showBackName(id);


    }

//        查询当前分类下子分类 pj
//    @RequestMapping("findBackCourseType.do")
//    @ResponseBody
//    public PageInfo<CourseType> findBackCourseType(@RequestParam("parent_id") int parent_id, @RequestParam("pageNo") String page) {
//        int pageno = StringUtil.getInt(page);
//        PageInfo<CourseType> info = courseTypeService.findBackCourseType(parent_id, pageno);
//        return info;
//    }

    //    修改分类 pj
    @RequestMapping("/modifyCourseTypeName.do")
    @ResponseBody
    public AjaxResult modifyCourseTypeName(CourseType courseType) {
        AjaxResult result = new AjaxResult(false, "", null);
        int i = courseTypeService.modifyCourseTypeName(courseType);
        if (0 == i) {
            result.setMsg("修改失败");
        }
        result.setMsg("修改成功");
        return result;

    }

    //    添加分类  pj
    @RequestMapping("addBackType.do")
    @ResponseBody
    public AjaxResult addBackType(CourseType courseType) {
        AjaxResult result = courseTypeService.addBackType(courseType);
        return result;
    }

    //后台修改用户状态禁用-启用 pj
    @RequestMapping("/toggleStatus.do")
    @ResponseBody
    public AjaxResult toggleStatus(Integer id, Integer status) {
        AjaxResult result = courseTypeService.toggleStatus(id, status);
        return result;
    }

    @RequestMapping("/findBackCourseType.do")
    @ResponseBody
    public PageInfo<CourseType> findBackCourseType(HttpServletRequest request, HttpSession session) {
        PageInfo<CourseType> info = new PageInfo<>();
        String pageNo = request.getParameter("pageNo");
        String parent_id1 = request.getParameter("parent_id");
        Integer page = null;
        Integer parent_id = null;
        if(pageNo != null){
            page = Integer.valueOf(pageNo);
            parent_id = Integer.valueOf(parent_id1);
            info = courseTypeService.findBackCourseType(page,parent_id);
            session.setAttribute("courseTypeList",courseTypeService.findBackCourseTypeAll());
            return  info;
        } else {
            parent_id = Integer.valueOf(parent_id1);
            PageInfo<CourseType> courseTypeA = courseTypeService.findBackCourseTypeA(parent_id);
            return courseTypeA;
        }
    }

}
